//
// StringTest.m -- test out the String class
//		Written by Don Yacktman Copyright (c) 1993 by Don Yacktman.
//				Version 1.0.  All rights reserved.
//
//		This notice may not be removed from this source code.
//
//	This program is included in the MiscKit by permission from the author
//	and its use is governed by the MiscKit license, found in the file
//	"LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
//	for a list of all applicable permissions and restrictions.
//	

#import <appkit/appkit.h>
#import <misckit/misckit.h>
#import <stdio.h>

void main()
{
	id string1, string2, string3, string4, string5, string6, string7; int i;
	string1 = [[[MiscString alloc] init] setStringValue:"The first string."];
	string2 = [[MiscString alloc] initString:"01234567890123456789"];
	printf("string1 = \"%s\", length = %d.\n", [string1 stringValue],
			[string1 length]);
	printf("string2 = \"%s\", length = %d.\n", [string2 stringValue],
			[string2 length]);
	string3 = [string1 copy];
	[string3 concatenate:string2];
	printf("String1 + String2 = \"%s\", length = %d.\n",
			[string3 stringValue], [string3 length]);
	printf("String1 = \"%s\", length = %d.\n", [string1 stringValue],
			[string1 length]);
	[string3 free];
	printf("string1!=string1: %d, string1!=string2: %d\n",
			[string1 compareTo:string1], [string1 compareTo:string2]);
	string3 = [string2 left:5];
	string4 = [string2 right:5];
	string5 = [string2 midFrom:5 to:10];
	string6 = [string2 midFrom:6 length:5];
	printf("left5 = %s right5 = %s mid5to10 = %s mid6len5 = %s\n",
			[string3 stringValue], [string4 stringValue],
			[string5 stringValue], [string6 stringValue]);
	[string3 free]; [string4 free]; [string5 free];
	[string6 free]; string6 = nil;
	string5 = [[MiscString alloc] initString:"first"];
	string3 = [string1 subStringLeft:string5];
	string4 = [string1 subStringRight:string5];
	printf("Substrings from string1 and \"first\"...Left:\"%s\" Right:\"%s\"\n",
			[string3 stringValue], [string4 stringValue]);
	[string5 setStringValue:"don:*:23:21:Don Yacktman:/LocalLibrary/Users/don:/bin/csh"];
	printf("Extract parts from \"%s\" delimiting with ':':\n",
			[string5 stringValue]);
	printf("    Part    #3 is \"%s\"\n",
			[[string5 extractPart:2 useAsDelimiter:':'] stringValueAndFree]);
	printf("    Part   #10 is \"%s\"\n",
			[[string5 extractPart:9 useAsDelimiter:':'] stringValueAndFree]);
	printf("Return was %s.\n", [string5 extractPart:10 useAsDelimiter:':']
			? "empty object" : "nil");
	printf("    First part is \"%s\"\n",
			[[string5 extractPart:DAY_FIRST useAsDelimiter:':'] stringValueAndFree]);
	printf("    Last  part is \"%s\"\n",
			[[string5 extractPart:DAY_LAST useAsDelimiter:':'] stringValueAndFree]);
	printf("Extract all parts with while loop:\n"); i = 0;
	while (string6 = [string5 extractPart:i useAsDelimiter:':']) {
		printf("    part #%d is: \"%s\".\n", i++, [string6 stringValueAndFree]);
	}
	[string5 setStringValue:"/Net/darth/Users/don/Projects/daymisckit_proj/daymisckit-1/DAYString.m"];
	printf("Given:        %s\n", [string5 stringValue]);
	printf("    Path is:  %s\n", [[string5 pathName] stringValueAndFree]);
	printf("Filename is:  %s\n", [[string5 fileName] stringValueAndFree]);

	//Carl's tests...
	string7 = [MiscString newWithString:"   Hello, how are you?   "];
	printf("Carl's string: \"%s\", length = %d\n",
			[string7 stringValue], [string7 length]);
	printf("Numwords: %d\n",[string7 numWords]);
	printf("3rd word: \"%s\"\n",[[string7 wordNum:2] stringValueAndFree]);
	printf("trimmed: \"%s\"\n",[[string7 trimSpaces] stringValue]);
	printf("Numwords: %d\n",[string7 numWords]);
	printf("Reversed: \"%s\"\n",[[string7 reverse] stringValue]);
	printf("Reversed again: \"%s\"\n",[[string7 reverse] stringValue]);
	printf("ToUpper:  \"%s\"\n",[[string7 toUpper] stringValue]);
	printf("ToLower:  \"%s\"\n",[[string7 toLower] stringValue]);
	printf("Inserting \"Oh! \" at 0:  \"%s\"\n",
			[[string7 insert:"Oh! " at:0] stringValue]);
	printf("Char at 0: '%c'\n",[string7 charAt:0]);
	printf("ReplaceFrom 0to2 with \"YOHO!\":  \"%s\"\n",
			[[string7 replaceFrom:0 to:2 with:"YOHO!"] stringValue]);
	printf("spotOf 'o': %d\n",[string7 spotOf:'o']);
	printf("spotOf 'o' nocase: %d\n",
			[string7 spotOf:'o' caseSensitive:NO]);
	printf("rspotOf 'O': %d\n",[string7 rspotOf:'O']);
	printf("rspotOf 'O' nocase: %d\n",
			[string7 rspotOf:'O' caseSensitive:NO]);
	printf("10th rspotOf 'O': %d\n",[string7 rspotOf:'O' occurrenceNum:10]);
	printf("replace \"YOHO!\" with \"My!\": \"%s\"\n",
			[[string7 replace:"YOHO!" with:"My!"] stringValue]);
	printf("String now is \"%s\"\n",
			[[string7 setStringValue:"  Hello,   I    am  fine today.  Right?  "]
			stringValue]);
	printf("Squashed: \"%s\"\n",[[string7 squashSpaces] stringValue]);
	printf("endcmp \"Right?\": %d\n",[string7 endcmp:"Right?"]);
	printf("endcasecmp \"RIGHT?\": %d\n",[string7 endcasecmp:"RIGHT?"]);
	printf("endcmp \"RIGHT?\": %d\n",[string7 endcmp:"RIGHT?"]);
	printf("endcasecmp n:3 \"nacHT?\": %d\n",
			[string7 endcasecmp:"nacHT?" n:3]);
	[string7 replaceCharAt:0 withChar:'J'];
	printf("replaceCharAt:0 withChar:'J': \"%s\"\n",
			[string7 stringValue]);
	printf("insertChar 'a' at 1: \"%s\"\n",
			[[string7 insertChar:'a' at:1] stringValue]);
	// tests for v1.3:
	[string1 setStringValue:"not empty"];
	printf("Not empty (\"%s\"): %s  (length = %d)\n", [string1 stringValue],
			([string1 emptyString] ? "YES" : "NO"), [string1 length]);
	[string1 freeString];
	printf("Empty (\"%s\"): %s  (length = %d)\n", [string1 stringValue],
			([string1 emptyString] ? "YES" : "NO"), [string1 length]);
	[string1 setStringValue:"Something wicked this way comes."];
	printf("Replace all 'i' with '!':  \"%s\".\n",
			[[string1 replaceEveryOccurrenceOfChar:'i' withChar:'!']
					stringValue]);
	[string1 setStringValue:"One "]; [string7 setStringValue:"one "];
	[string2 setStringValue:"Two "]; [string3 setStringValue:"Three "];
	[string4 setStringValue:"Four "]; [string5 setStringValue:"Five"];
	[string7 catStrings:"two ", "three ", "four ", "five", NULL];
	printf("catStrings:  \"%s\".\n", [string7 stringValue]);
	[string1 concatenateStrings:string2, string3, string4, string5, nil];
	printf("concatenateStrings:  \"%s\".\n", [string1 stringValue]);

	printf("String now is \"%s\"\n",
			[[string1 setStringValue:"Something wicked this way comes."]
			stringValue]);
	printf("(All tests below restart with above string)\n");
	printf("Number of 's': %d\n", [string1 numOfChar:'s']);
	printf("Number of 's' nocase: %d\n", [string1 numOfChar:'s' caseSensitive:NO]);

	printf("Replace all 's' with '!':  \"%s\".\n",
			[[string1 replaceEveryOccurrenceOfChar:'s' withChar:'!']
			stringValue]);
	[string1 setStringValue:"Something wicked this way comes."];
	printf("Replace all 's' with 's' nocase:  \"%s\".\n",
			[[string1 replaceEveryOccurrenceOfChar:'s' withChar:'s'
				caseSensitive:NO] stringValue]);

	[string1 setStringValue:"Something wicked this way comes."];
	printf("Replace all 's' with \"Hi!\":  \"%s\".\n",
			[[string1 replaceEveryOccurrenceOfChar:'s'
			with:"Hi!"] stringValue]);

	[string1 setStringValue:"Something wicked this way comes."];
	printf("Replace all 's' with \"Hi!\" nocase:  \"%s\".\n",
			[[string1 replaceEveryOccurrenceOfChar:'s'
			with:"Hi!" caseSensitive:NO] stringValue]);
	
	[string1 setStringValue:"Something wicked this way comes."];
	printf("Replace all 's' with \"\" nocase:  \"%s\".\n",
			[[string1 replaceEveryOccurrenceOfChar:'s'
			with:"" caseSensitive:NO] stringValue]);
	
	[string1 setStringValue:"Something wicked this way comes."];
	printf("numOfChars 'siw' nocase: %d\n",
			[string1 numOfChars:"siw" caseSensitive:NO]);
	
	[string1 setStringValue:"Something wicked this way comes."];
	printf("spotOfChars 'siw': %d\n",
			[string1 spotOfChars:"siw"]);
	
	[string1 setStringValue:"Something wicked this way comes."];
	printf("spotOfChars 'siopen w' nocase: %d\n",
			[string1 spotOfChars:"siw" caseSensitive:NO]);
	
	[string1 setStringValue:"Something wicked this way comes."];
	printf("spotOfChars 'siw' #3 nocase: %d\n",
			[string1 spotOfChars:"siw" occurrenceNum:3 caseSensitive:NO]);
	
	[string1 setStringValue:"Something wicked this way comes."];
	printf("Replace all 'siw' with '!' nocase:  \"%s\".\n",
			[[string1 replaceEveryOccurrenceOfChars:"siw"
			withChar:'!' caseSensitive:NO] stringValue]);
	
	[string3 free]; [string4 free]; [string5 free];
	[string1 free]; [string2 free]; [string7 free];
	exit(0);
}
